package com.shiqi.bio.version4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author shiqi
 * @version 1.0.0
 * @ClassName Server.java
 * @Description
 * @createTime 2022-05-16
 */
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        HandlerSocketThreadPool handlerSocketThreadPool = new HandlerSocketThreadPool(3, 1000);
        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("有人上线了");
            // 每次收到一个客户端的socket请求，都需要为这个客户端分配一个
            // 独立的线程 专门负责对这个客户端的通信！！
            handlerSocketThreadPool.execute(new ReaderClientRunnable(socket));
        }

    }
}

class ReaderClientRunnable implements Runnable {

    private Socket socket;

    public ReaderClientRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println("服务端收到客户端的信息：" + line);
            }
        } catch (Exception e) {
            System.out.println("下线");
        }
    }
}
